package com.example.parallelandroid.novicedialog;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.parallelandroid.R;


public class PickDialog extends Dialog implements View.OnTouchListener {

    public PickDialog(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public PickDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    protected PickDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initView(context);
    }



    private ImageView novice;

    private ImageView next, last;
    private ScaleAnimation animation1, animation2;

    private WhichClick click = WhichClick.NEXT;

    private LinearLayout bg;

    private IPickDialog iPickDialog;

    public void setiPickDialog(IPickDialog iPickDialog) {
        this.iPickDialog = iPickDialog;
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.novicedialog_layout, null);
        setContentView(view);
        novice = findViewById(R.id.iv_novice);
        next = findViewById(R.id.next);
        last = findViewById(R.id.last);
        bg = findViewById(R.id.bg);
        next.setOnTouchListener(this);
        last.setOnTouchListener(this);
        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScaleAnimation animation = new ScaleAnimation(
                        1.0f, 0f, 1.0f, 1.0f,
                        Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f
                );
                animation.setDuration(200);
                bg.startAnimation(animation);
                iPickDialog.onLast();
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScaleAnimation animation = new ScaleAnimation(
                        1.0f, 0f, 1.0f, 1.0f,
                        Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f
                );
                animation.setDuration(200);
                bg.startAnimation(animation);
                iPickDialog.onNext();
            }
        });

        animation1 = new ScaleAnimation(
                1.0f, 1.5f, 1.0f, 1.5f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        );
        animation1.setDuration(200);
        animation1.setFillAfter(true);

        animation2 = new ScaleAnimation(
                1.0f, 1f, 1.0f, 1f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        );
        animation2.setDuration(200);
        animation2.setFillAfter(true);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                switch (v.getId()) {
                    case R.id.next:
                        next.startAnimation(animation1);
                        click = WhichClick.NEXT;
                        break;
                    case R.id.last:
                        last.startAnimation(animation1);
                        click = WhichClick.LAST;
                        break;
                }
                break;
            case MotionEvent.ACTION_UP:
                switch (click) {
                    case LAST:
                        last.startAnimation(animation2);
                        break;
                    case NEXT:
                        next.startAnimation(animation2);
                        break;
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    public void initImage(int src) {
        novice.setImageResource(src);
    }

    enum WhichClick {
        LAST, NEXT
    }
}
